package com.jetgege.smartbox.Tools;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by JJT-ssd on 2017/4/25.
 */

public class AutomaticRectangle  extends View {


    private int valueLevel = 0;      //方法传进来的表示进度
    private Paint mBasePaint, mRectanglePaint;
    /**
     * 线的宽度
     */


    /**
     * 要画图形，最起码要有三个对象：
     * 1.颜色对象 Color
     * 2.画笔对象 Paint
     * 3.画布对象 Canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        initPaint();
        //这是圆心的坐标，这个是在xml设置引用的这个布局控件的大小的中心点到这个布局控件的最左边
        int width = getWidth();
        int height = getHeight();
        float lineWidth = width/33;
        //设置宽度为20dp
        mBasePaint.setStrokeWidth(lineWidth);
        mRectanglePaint.setStrokeWidth(lineWidth);
        //初始化圆弧所需条件（及设置圆弧的外接矩形的四边）
        for (int i=1;i<17;i++){
            canvas.drawLine(i*2*lineWidth,0,i*2*lineWidth,height,mBasePaint);
        }
        if (valueLevel>=17)valueLevel=17;
        for (int i=1;i<valueLevel+1;i++){
            canvas.drawLine(i*2*lineWidth,0,i*2*lineWidth,height,mRectanglePaint);
        }
        super.onDraw(canvas);
    }


    /**
     * 暴露一个方法给外部调用来调整进度
     * @param valueLevel 取值为1-17
     */
    public void rePaint(int valueLevel)
    {
        this.valueLevel = valueLevel;
        invalidate();
    }



    public AutomaticRectangle (Context context) {super(context);}
    private void initPaint() {

        if (mBasePaint!= null) return;

        //初始底圆弧笔
        mBasePaint = new Paint();
        //设置抗锯齿，优化绘制效果的精细度
        mBasePaint.setAntiAlias(true);
        //设置图像抖动处理,也是用于优化图像的显示效果
        mBasePaint.setDither(true);
        //设置画笔的颜色
        mBasePaint.setColor(Color.parseColor("#7AB6C4"));
        //设置画笔的风格为实心
        mBasePaint.setStyle(Paint.Style.FILL);

        //初始化中心圆的黑色笔
        mRectanglePaint = new Paint();
        mRectanglePaint.setAntiAlias(true);
        mRectanglePaint.setDither(true);
        mRectanglePaint.setColor(Color.parseColor("#1FB5B2"));
        mRectanglePaint.setStyle(Paint.Style.FILL);

    }

    public AutomaticRectangle (Context context, AttributeSet attrs) {super(context, attrs );}
    public AutomaticRectangle (Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle );}
}